<?php
    class Adresse {
        //Alias sur table : a
        public $id_adresse;
        public $client;
        public $adr1;
        public $adr2;
        public $ville;
        public $cp;
        public $pays;
        public $supprime;
        public $valide;


        //Constructeur complet
        //(Client)client
        public function __construct($id_adresse, $client, $adr1, $adr2, $ville, $cp, $pays, $supprime, $valide) {
            $this->setId($id_adresse);
            $this->setClient($client);
            $this->setAdr1($adr1);
            $this->setAdr2($adr2);
            $this->setVille($ville);
            $this->setCp($cp);
            $this->setPays($pays);
            $this->setSupprime($supprime);
            $this->setValide($valide);
        }

        //Retourne l'intitulé de l'adresses
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->adr1 . ' ' . $this->adr2 . ' ' . $this->cp . ' ' . $this->ville;
        }
        
        //Ajoute une adresse à la BDD
        //Paramètre : aucun
        //Retourne (Client)
        public function add() {
            $sql =  'INSERT INTO adresse (id_client, adr1, adr2, ville, cp, pays, supprime, valide) VALUES
                    ('.intval($this->client->getId()).', "'.Db::insertion($this->adr1).'", "'.Db::insertion($this->adr2).'", 
                    "'.Db::insertion($this->ville).'", "'.Db::insertion($this->cp).'", "'.Db::insertion($this->pays).'", 
                    '.intval($this->supprime).', '.intval($this->valide).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
		
        //Met à jour une adresse dans la BDD
        //Paramètre : aucun
        //Retourne (Adresse)
        public function update() {
            $sql =  'UPDATE adresse
                    SET adr1 = "'.Db::insertion($this->adr1).'", 
                            adr2 = "'.Db::insertion($this->adr2).'", 
                            ville = "'.Db::insertion($this->ville).'", 
                            cp = "'.Db::insertion($this->cp).'", 
                            pays = "'.Db::insertion($this->pays).'", 
                            supprime = '.intval($this->supprime).', 
                            valide = '.intval($this->valide).'
                    WHERE
                        id_adresse = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
		
        //Désactive une adresse dans la BDD
        //Paramètre : aucun
        //Retourne (Adresse)
        public function suppr() {
            $this->setValide(false);
            return $this->update();
        }

        //Supprime définitivement une adresse pour l'utilisateur
        //Paramètre : aucun
        //Retourne (Adresse)
        public function delete() {
            $this->setSupprime($false);
            return $this->update();
        }
		
        //Crée un objet Adresse grâce à son ID
        //Paramètre : (int)id
        //Retourne (Adresse)
        public static function rechercheAdresse($id) {
            $sql =  'SELECT
                        a.id_adresse as aid_adresse, a.adr1 as aadr1, a.adr2 as aadr2, a.ville as aville, a.cp as acp, a.pays as apays, a.supprime as asupprime, 
                        a.valide as avalide, 
                        cl.id_client as clid_client, cl.login as cllogin, cl.nom as clnom, cl.prenom as clprenom, 
                        cl.mdp as clmdp, cl.telephone as cltelephone, cl.mobile as clmobile, cl.email as clemail, 
                        cl.date_naissance as cldate_naissance, cl.date_ajout as cldate_ajout, cl.date_modif as cldate_modif, 
                        cl.valide as clvalide,
                        pro.id_profil as proid_profil, pro.libelle as prolibelle, pro.valide as provalide,
                        civ.id_civilite as civid_civilite, civ.libelle as civlibelle, civ.diminutif as civdiminutif, civ.valide as civvalide
                    FROM adresse a
                    LEFT JOIN client cl ON a.id_client = cl.id_client 
                    LEFT JOIN profil pro ON cl.id_profil = pro.id_profil
                    LEFT JOIN civilite civ ON cl.id_civilite = civ.id_civilite
                    WHERE
                        a.id_adresse = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $profil = new Profil($p['proid_profil'], $p['prolibelle'], $p['provalide']);
                $civilite = new Civilite($p['civid_civilite'], $p['civlibelle'], $p['civdiminutif'], $p['civvalide']);
                $client = new Client($p['clid_client'], $civilite, $profil, $p['cllogin'], $p['clmdp'], $p['clnom'], $p['clprenom'], 
                                    $p['cltelephone'], $p['clmobile'], $p['clemail'], $p['cldate_naissance'], $p['cldate_ajout'], 
                                    $p['cldate_modif'], $p['clvalide']);
                return new Adresse($p['aid_adresse'], $client, $p['aadr1'], $p['aadr2'], $p['aville'], $p['acp'], $p['apays'], 
                                    $p['asupprime'], $p['avalide']);
            }

            return null;
        }
		
        //GETTERS ET SETTERS
        public function getId() {return $this->id_adresse;}
        public function getClient() {return $this->client;}
        public function getAdr1() {return $this->adr1;}
        public function getAdr2() {return $this->adr2;}
        public function getAdresses() {return $this->adr1 . ' ' . $this->adr2;}
        public function getVille() {return $this->ville;}
        public function getCp() {return $this->cp;}
        public function getPays() {return $this->pays;}
        public function getSupprime() {return $this->supprime;}
        public function getValide() {return $this->valide;}

        public function setId($temp) {$this->id_adresse = intval($temp);}
        public function setClient($temp) {$this->client = $temp;}
        public function setAdr1($temp) {$this->adr1 = $temp;}
        public function setAdr2($temp) {$this->adr2 = $temp;}
        public function setVille($temp) {$this->ville = $temp;}
        public function setCp($temp) {$this->cp = $temp;}
        public function setPays($temp) {$this->pays = $temp;}
        public function setSupprime($temp) {$this->supprime = intval($temp);}
        public function setValide($temp) {$this->valide = intval($temp);}
    }

?>